package com.maila.simplerecord.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.maila.simplerecord.R;
import com.maila.simplerecord.data.ProjectProgressdata;
import com.maila.simplerecord.data.Projectdata;
import com.maila.simplerecord.utils.PermissionUtils;
import com.zhy.autolayout.utils.AutoUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/6/1.
 */

public class HomeProjectAdapter extends BaseAdapter {
    Context context;
    List<Projectdata> mdata;

    public HomeProjectAdapter(Context context) {
        this.context = context;
        mdata = new ArrayList<>();
    }

    public List<Projectdata> getMdata() {
        return mdata;
    }

    public void setMdata(List<Projectdata> mdata) {
        this.mdata = mdata;
        notifyDataSetChanged();
    }
    public void addAll(Collection<Projectdata> collection) {
        if (isEmpty()) {
            mdata.addAll(collection);
            notifyDataSetChanged();
        } else {
            mdata.addAll(collection);
        }
    }

    @Override
    public int getCount() {
        return mdata.size();
    }

    @Override
    public Projectdata getItem(int position) {
        return mdata.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, final ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.card_new_item, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
            AutoUtils.autoSize(convertView);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.clientNum.setText(mdata.get(position).client_phone);
        holder.clientNum.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mdata.get(position).getClient_phone() != null) {
                    PermissionUtils permissionUtils = new PermissionUtils((Activity) context);
                    if (permissionUtils.requesCallPhonePermissions(200)) {

                        Intent intent = new Intent(Intent.ACTION_DIAL);
                        Uri data = Uri.parse("tel:" + mdata.get(position).getClient_phone());
                        intent.setData(data);
                        context. startActivity(intent);
                    }
                }
            }
        });
        if(mdata.get(position).getImg_paths()!=null&&!"".equals(mdata.get(position).getImg_paths().split(",")[0])){
         Glide.with(context).load(Uri.parse("file://"+mdata.get(position).getImg_paths().split(",")[0])).into(holder.portrait);
        }else {
            holder.portrait.setImageResource(R.drawable.recodedemo);
        }
        if(mdata.get(position).note!=null){
            holder.title.setText(mdata.get(position).note);
        }else {
            holder.title.setText("暂无明日备注");
        }

        holder.projectName.setText(mdata.get(position).project_name);
        List<ProjectProgressdata> pgdatas=ProjectProgressdata.getAll(mdata.get(position).getPid());
        if(pgdatas.size()>0){
            if(pgdatas.size()==1){
                holder.ll_progress2.setVisibility(View.GONE);
                holder.ll_progress1.setVisibility(View.VISIBLE);
                holder.subjectTime1.setText(pgdatas.get(0).subject_time);
                holder.subjectContent1.setText(pgdatas.get(0).subject_content);
            }else if(pgdatas.size()>1){
                holder.ll_progress1.setVisibility(View.VISIBLE);
                holder.ll_progress2.setVisibility(View.VISIBLE);
                holder.subjectTime1.setText(pgdatas.get(0).subject_time);
                holder.subjectContent1.setText(pgdatas.get(0).subject_content);
                holder.subjectTime2.setText(pgdatas.get(1).subject_time);
                holder.subjectContent2.setText(pgdatas.get(1).subject_content);
            }
        }else {
            holder.ll_progress1.setVisibility(View.GONE);
            holder.ll_progress2.setVisibility(View.GONE);
        }
        convertView.setLongClickable(true);
        convertView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Toast.makeText(context,"delete",Toast.LENGTH_SHORT).show();
                return false;
            }
        });
        holder.removeTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showMessageOK("确定取消首页显示?", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mdata.get(position).top=0;
                        mdata.get(position).save();
                        mdata.remove(position);
                        notifyDataSetChanged();
                        Toast.makeText(context,"已去除",Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
        return convertView;
    }

    public boolean isEmpty() {
        return mdata.isEmpty();
    }
    public void remove(int index) {
        if (index > -1 && index < mdata.size()) {
            mdata.remove(index);
            notifyDataSetChanged();
        }
    }

    public void clear() {
        mdata.clear();
        notifyDataSetChanged();
    }

    private void showMessageOK(String message, DialogInterface.OnClickListener okListener) {
        new AlertDialog.Builder(context)
                .setMessage(message)
                .setPositiveButton("确定", okListener)
                .create()
                .show();
    }

    static class ViewHolder {
        @BindView(R.id.portrait)
        ImageView portrait;
        @BindView(R.id.project_name)
        TextView projectName;
        @BindView(R.id.client_num)
        TextView clientNum;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.subject_time1)
        TextView subjectTime1;
        @BindView(R.id.subject_content1)
        TextView subjectContent1;
        @BindView(R.id.subject_time2)
        TextView subjectTime2;
        @BindView(R.id.subject_content2)
        TextView subjectContent2;
        @BindView(R.id.remove_top)
        ImageView removeTop;
        @BindView(R.id.ll_progress1)
        LinearLayout ll_progress1;
        @BindView(R.id.ll_progress2)
        LinearLayout ll_progress2;


        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
